home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / cycles / bad.c next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.8 KB  |  224 lines

  1.  
  2. #include "cycles.h"
  3. #define SIGN(x)   ( (x)<0.0 ? -1.0 : ((x)>0.0?1.0:0.0) )
  4.  
  5. CYCLE *find_nearest_person(CYCLE *);
  6. int person_elsewhere();
  7.  
  8. extern float vec[4][2], speed_fac;
  9. extern float speed_fac;
  10. extern int used[CYCLES];
  11. extern CYCLE bike[CYCLES];
  12. extern POINT up_hole[LEVELS], down_hole[LEVELS];
  13.  
  14. /*
  15.  * this routine contains all bad-guy evasive behaviour
  16.  */
  17. void move_cycles(CYCLE *C) {
  18.     int l, r, next_level, target_level, levels_away, behave, close_id;
  19.     CYCLE *target;
  20.     POINT *hole;
  21.     float front, left, right, margin, jump_dist;
  22.     float wall_prox, jump_height, jump_speed, zdes, xdes;
  23.  
  24.     /* move levels if we're over a hole */
  25.     if ((next_level = fall_down_hole(C)) != -1)
  26.     new_level(C, next_level);
  27.  
  28.     margin = (CYCLE_NOSE + 2.5*C->step*speed_fac);
  29.     behave = C->behave;
  30.  
  31.     /* find nearest non falling PERSON */
  32.     if ((target = find_nearest_person(C)) != (CYCLE *)NULL) {
  33.     /* ZDES & XDES are the desired directions to cut off the target */
  34.     xdes = target->origin.x - (C->origin.x-1.5*margin*vec[C->vec_ptr][0]);
  35.     zdes = target->origin.z - (C->origin.z-1.5*margin*vec[C->vec_ptr][1]);
  36.     }
  37.     else if ((target_level = person_elsewhere()) != -1) {
  38.     /* look for another level to go to */
  39.     levels_away = target_level - C->level;
  40.     if ( (levels_away + LEVELS)%LEVELS < LEVELS/2 ) {
  41.         if (levels_away > 0)
  42.         hole = &up_hole[C->level];
  43.         else
  44.         hole = &down_hole[C->level];
  45.     }
  46.     else {
  47.         if (levels_away < 0)
  48.         hole = &up_hole[C->level];
  49.         else
  50.         hole = &down_hole[C->level];
  51.     }
  52.     xdes = hole->x - C->origin.x;
  53.     zdes = hole->z - C->origin.z;
  54.     }
  55.     else {
  56.     /* no one to head for so meander */
  57.     behave = 0;
  58.     }
  59.  
  60.     zdes = SIGN(zdes);
  61.     xdes = SIGN(xdes);
  62.  
  63. #ifdef DEBUG
  64. printf("this is BAD %d\n", C->id);
  65. #endif
  66.  
  67.     front = block(C->id, C->origin.x, C->origin.z, C->vec_ptr, &close_id);
  68.  
  69. #ifdef DEBUG
  70. printf("BAD %d front: %g\n", C->id, front);
  71. #endif
  72.  
  73.     /* start the trail falling if hit something */
  74.     if ((front - CYCLE_NOSE) <= C->step*speed_fac && (C->origin.y < (HEIGHT + TRAIL_HEIGHT))) {
  75.     C->fall += speed_fac;
  76.     C->falling++;
  77.     C->who_we_hit = close_id;
  78.     }
  79.  
  80. #ifdef DEBUG
  81. if (C->fall) printf("BAD %d FALLING\n", C->id);
  82. #endif
  83.  
  84.     if (front < margin) {
  85.     l = C->vec_ptr + 1;
  86.     if (l == 4) l = 0;
  87.     r = C->vec_ptr - 1;
  88.     if (r == -1) r = 3;
  89. #ifdef DEBUG
  90. printf("BAD %d sides:\n", C->id);
  91. #endif
  92.     left = block(C->id, C->origin.x, C->origin.z, l, &close_id);
  93.     right = block(C->id, C->origin.x, C->origin.z, r, &close_id);
  94. #ifdef DEBUG
  95. printf("BAD %d finished sides: left %g right %g\n", C->id, left, right);
  96. #endif
  97.     jump_dist = 0.0;
  98.     jump_height = C->origin.y;
  99.     jump_speed = C->jump_speed*speed_fac;
  100.     wall_prox = DIM - ABS(vec[C->vec_ptr][0]*C->origin.x)
  101.                     - ABS(vec[C->vec_ptr][1]*C->origin.z);
  102.     while (jump_height > (HEIGHT + TRAIL_HEIGHT)) {
  103.         jump_height += jump_speed*speed_fac;
  104.         jump_speed -= GRAVITY*speed_fac;
  105.         jump_dist += C->step*speed_fac;
  106.     }
  107.     
  108.     /*
  109.      * I think this means the bad guys can do turns and jumps
  110.      * at the same time!!! Oh well - I guess they need all
  111.      * the help they can get...
  112.      */
  113.     if ((left > margin) || (right > margin))
  114.         if (jump_dist < front)
  115.         /* if in attack mode (and blocked) turn towards the good guy */
  116.         if (behave) {
  117.             if (((left>margin)&&((vec[l][0]==xdes)||
  118.                 (vec[l][1]==zdes)))||(right<=margin))
  119.                 turn(C,1.0);
  120.             if (((right>margin)&&((vec[r][0]==xdes)||
  121.                 (vec[r][1]==zdes)))||(left<=margin))
  122.                 turn(C,-1.0);
  123.         } else
  124.             if (left < right)
  125.                 turn(C, -1.0);
  126.             else
  127.                 turn(C, 1.0);
  128.  
  129.     if ((jump_dist > front) && (wall_prox < margin))
  130.         if (left < right)
  131.         turn(C, -1.0);
  132.         else
  133.         turn(C, 1.0);
  134.  
  135.     if ((left < margin) && (right < margin) && !C->jump) jump(C);
  136.     if (C->step>MIN_STEP) C->step-=0.3*speed_fac;
  137.     }
  138.     else {
  139.     /* if unblocked, attack if possible */
  140.     if (behave && (zdes!=vec[C->vec_ptr][1])&&(xdes!=vec[C->vec_ptr][0])){
  141.         l=C->vec_ptr+1;
  142.         if(l==4)l=0;
  143.         r=C->vec_ptr-1;
  144.         if(r== -1) r=3;
  145.         left=block(C->id,C->origin.x,C->origin.z,l, &close_id);
  146.         right=block(C->id,C->origin.x,C->origin.z,r, &close_id);
  147.         if ((left>margin)&&((vec[l][0]==xdes)||(vec[l][1]==zdes))) {
  148.         turn(C,1.0);
  149.         }
  150.         if ((right>margin)&&((vec[r][0]==xdes)||(vec[r][1]==zdes))) {
  151.         turn(C,-1.0);
  152.         }
  153.     }
  154.     if (!behave) {
  155.         if (!(rand()%((int)(200.0*speed_fac)))) {
  156.         l=C->vec_ptr+1;
  157.         if(l==4)l=0;
  158.         r=C->vec_ptr-1;
  159.         if(r== -1) r=3;
  160.         left=block(C->id,C->origin.x,C->origin.z,l, &close_id);
  161.         right=block(C->id,C->origin.x,C->origin.z,r, &close_id);
  162.         if ((left > margin) || (right > margin))
  163.             if (left < right)
  164.             turn(C, -1.0);
  165.             else
  166.             turn(C, 1.0);
  167.         }
  168.     }
  169.     if ((front>0.66*DIM) && (C->step<MAX_STEP)) C->step += 0.1*speed_fac;
  170.     if ((front<0.25*DIM) && (C->step>MIN_STEP)) C->step -= 0.2*speed_fac;
  171.     }
  172.  
  173.     /* check we're not going too fast or slow */
  174.     if (C->step>MAX_STEP) C->step = MAX_STEP;
  175.     if (C->step<MIN_STEP) C->step = MIN_STEP;
  176.  
  177.     /*
  178.      * move last 'cos speed fac can vary greatly
  179.      * hence the stuff above can be wrong if we move first
  180.      */
  181.     mov(C);
  182.  
  183.     /* check if outside grid and fall if are */
  184.     check_outside(C);
  185. }
  186.  
  187.  
  188. /* find nearest non falling PERSON to cycle C */
  189. CYCLE *find_nearest_person(CYCLE *C) {
  190.     int i;
  191.     float dx, dz, dist2, min_d2;
  192.     CYCLE *nearest;
  193.  
  194.     min_d2 = 1000.0*DIM;
  195.     nearest = (CYCLE *)NULL;
  196.  
  197.     for (i = 0; i < CYCLES; i++) {
  198.     if (used[i] && bike[i].alive && bike[i].type == PERSON
  199.      && bike[i].falling == 0 && bike[i].level == C->level) {
  200.         dx = bike[i].origin.x - C->origin.x;
  201.         dz = bike[i].origin.z - C->origin.z;
  202.         dist2 = dx*dx + dz*dz;
  203.         if (dist2 < min_d2) {
  204.         nearest = &bike[i];
  205.         min_d2 = dist2;
  206.         }
  207.     }
  208.     }
  209.  
  210.     return (nearest);
  211. }
  212.  
  213.  
  214. /* returns a level number where there is a person */
  215. int person_elsewhere() {
  216.     int i;
  217.  
  218.     for (i = 0; i < CYCLES; i++)
  219.     if (used[i] && bike[i].alive && bike[i].type == PERSON && bike[i].falling == 0)
  220.         return(bike[i].level);
  221.  
  222.     return (-1);
  223. }
  224.